<?php
	$name = $_POST['email'];
	$email_type = $_POST['email_type'];
	$output = $_POST['output'];
	$fileName = "../data/$email_type/".$name;
	$fileContent = $output;
	$handle = fopen($fileName, "w");
	fwrite($handle, $fileContent);
	fclose($handle);
	header("Location: /generic-email-generator");
	
	function getFileName($name){
		$exist = file_exists($name . ".html");
		if($exist){
			if(is_numeric(substr($name, -1, 1))){
				$num = substr($name, -1, 1);
				$name = str_replace($num, $num+1, $name);
				$toReturn = getFileName($name);
			}else{
				$toReturn = getFileName($name . "2");
			}
			return $toReturn;
		}else{
			return $name;
		}
	}
?>
